<?php
namespace app\api\controller;
 
use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use app\api\model\Token;
use think\facade\Db;
use app\api\model\User as ModelUser;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "user_register",
        'user_login',
        
        
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //注册
    public function user_register()
    {
        $param = $this->request->param();
        if(empty($param["account_number"])){
            $this->error("账号不能为空");
        }
        if(empty($param["password"])){
            $this->error("密码不能为空");
        }
        $user = Db::name("user")->where("phone",$param["account_number"])->find();
        if(!empty($user)){
            $this->error("此账号已注册，请重新注册");
        }
        Db::name("user")->insert([
            "phone"=>$param["account_number"],
            "password"=>password($param["password"])
        ]);
        $this->success('注册成功');
    }

    //登录
    public function user_login(){
        $param = $this->request->param();
        if (empty($param['account_number'])) {
            $this->error("登陆失败,账号不能为空");
        }
        if (empty($param['password'])) {
            $this->error("登陆失败,密码不能为空");
        }
        $flag = Db::name("user")->where("phone",$param["account_number"])->find();
        if(empty($flag)){
            $this->error("登陆失败,账户不存在");
        }
     
        if($flag["password"] != password($param["password"])){
            $this->error("登陆失败,密码错误");
        }
        $token = Auth::makeToken();
        $userToken = new Token();
        $userToken->setToken($token, $flag["id"], $this->method);
        $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'user_id' => $flag["id"], 'userArr' => $flag]);
    }

    //智能问答
    public function question_answering(){
        $param = $this->request->param();
        if (empty($param['content'])) {
            $this->error("询问内容不能为空");
        }
     
        $url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
        
        $method = 'POST';
        $header = [
            "Content-Type:application/json",
            "Authorization:Bearer sk-52307f9c5528452aac5778c0c5eb2a3e"
        ];

        $qwe = [
            "model"=>"qwen-turbo",
            "input"=>[
                "messages"=>[
                    [
                        "role"=> "system",
                        "content"=> "You are a helpful assistant."
                    ],
                    [
                        "role"=>"user",
                        "content"=>$param['content']
                    ]
                ]
            ]
        ]; 
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(isset($data["request_id"])){
            $this->success("获取成功",$data["output"]["text"]);
       }else{
            $this->error($data["message"]);
       }
   
    }

    //新建会话
    public function add_session(){
        $param = $this->request->param();
        if (empty($param['title'])) {
            $this->error("标题不能为空");
        }
        if (empty($param['desc'])) {
            $this->error("内容不能为空");
        }
        $flag = Db::name("user_session")->where("uid",$this->uid)->where("title",$param["title"])->find();
        if (!empty($flag)) {
            $this->error("标题重复，请重新输入");
        }
        $param["uid"] = $this->uid;
        $param["create_time"] = time();
        Db::name("user_session")->insert($param);
        $this->success("新增成功");
    }

    //编辑会话
    public function edit_session(){
        $param = $this->request->param();
        if (empty($param['ses_id'])) {
            $this->error("会话id不能为空");
        }
        $ses_id = $param["ses_id"];
        unset($param["ses_id"]);
        Db::name("user_session")->where("id",$ses_id)->update($param);
        $this->success("编辑成功");
    }

    //删除会话
    public function del_session(){
        $param = $this->request->param();
        if (empty($param['ses_id'])) {
            $this->error("会话id不能为空");
        }
        Db::name("user_session")->where("id",$param["ses_id"])->delete();
        $this->success("删除成功");
    }
    
    //会话详情
    public function deial_session(){
        $param = $this->request->param();
        if (empty($param['ses_id'])) {
            $this->error("会话id不能为空");
        }
        $list = Db::name("user_session")->where("id",$param["ses_id"])->find();
        $this->success("获取成功",$list);
    }

    //会话列表
    public function list_session(){
        $list = Db::name("user_session")->where("uid",$this->uid)->select()->toArray();
        $this->success("获取成功",$list);
    }







 







   
}       
 